﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;

namespace CRM.Common
{
    public class XmlHelper
    {
        #region 泛型xml操作
        /// <summary>
        /// 根据xml的值  赋值给model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">xml的绝对路径</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public T GetXML<T>(string path)
        {
            try
            {
                XmlDocument document = new XmlDocument();

                document.Load(path);

                XmlNodeReader reader = new XmlNodeReader(document);

                XmlNodeList nodeList = document.DocumentElement.ChildNodes;

                //获取xml的值  加载进Dictionary中
                Dictionary<string, string> xmlModel = new Dictionary<string, string>();

                foreach (var item in nodeList)
                {
                    if (item.GetType().Name == "XmlElement")
                    {
                        XmlNode node = (XmlNode)item;
                        xmlModel[node.Name] = node.InnerText;
                    }
                }
                Type type = typeof(T);
                object models = Activator.CreateInstance(type);

                MethodInfo[] methods = type.GetMethods();

                var query = from n in methods
                            where n.Name.Contains("set")
                            select n;

                //给model赋值
                foreach (var item in query)
                {
                    object[] objArr = { xmlModel[item.Name.Substring(4)] };
                    item.Invoke(models, objArr);
                }

                return (T)models;
            }
            catch (Exception)
            {
                return default(T);
            }

        }

        /// <summary>
        /// xml赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetXML<T>(string path, T model)
        {
            bool isSuccess = false;
            try
            {
                Dictionary<string, string> xmlDic = new Dictionary<string, string>();

                MethodInfo[] methodInfos = model.GetType().GetMethods();

                var query = from n in methodInfos
                            where n.Name.Contains("get")
                            select n;

                foreach (var item in query)
                {
                    object[] objArr = { };
                    object obj = item.Invoke(model, objArr);

                    xmlDic.Add(item.Name.Substring(4), obj.ToString());
                }

                XmlDocument document = new XmlDocument();
                document.Load(path);
                XmlNodeList nodeList = document.DocumentElement.ChildNodes;

                for (int i = 0; i < nodeList.Count; i++)
                {
                    if (nodeList[i].GetType().Name == "XmlElement")
                    {
                        nodeList[i].InnerText = xmlDic[nodeList[i].Name];
                    }
                }

                document.Save(path);

                isSuccess = true;
            }
            catch (Exception e)
            {

            }

            return isSuccess;
        }
        #endregion
    }
}
